package online;

import java.io.IOException;
import java.net.*;
import java.util.Scanner;

public class UdpEchoClient {
    private DatagramSocket socket=null;
    private String serverIp;
    private int severPort;
    public UdpEchoClient(String serverIp,int severPort) throws SocketException {
        this.serverIp=serverIp;
        this.severPort=severPort;
        socket=new DatagramSocket();
    }
    public void start() throws IOException {
        Scanner scanner=new Scanner(System.in);
        while (true){
            System.out.println("->");
            String request=scanner.next();
            if (request.equals("exit")){
                System.out.println(request);
                return;
            }
            DatagramPacket requestPacket=new DatagramPacket(request.getBytes(),request.getBytes().length
            , InetAddress.getByName(serverIp),severPort);
            socket.send(requestPacket);
            DatagramPacket responsePacket=new DatagramPacket(new byte[4096],4096 );
            socket.receive(responsePacket);
            String response=new String(responsePacket.getData(),0,responsePacket.getLength());
            String log=String.format("req: %s; resp: %s\n",request,response);
            System.out.println(log);
        }
    }

    public static void main(String[] args) throws IOException {
        UdpEchoClient client=new UdpEchoClient("127.0.0.1",9090);
        client.start();
    }
}
